package com.tank.control;

import com.tank.entrity.Bullet;
import com.tank.entrity.GameObject;
import com.tank.entrity.Tank;

/**
 * 负责子弹和坦克的碰撞
 */
public class BulletTankCollider implements Collider {

    @Override
    public boolean collide(GameObject o1, GameObject o2) {
        if (o1 instanceof Bullet && o2 instanceof Tank) {
            Tank tank = (Tank) o2;
            Bullet bullet = (Bullet) o1;
            if (bullet.getPlayerId().equals(bullet.getId())) {
                return false;
            }
            if (bullet.getBulletRectangle().intersects(tank.getTankRectangle()) && bullet.getGroupType() != tank.getGroupType()) {
                tank.destroy();
                bullet.destroy();
                return true;
            }
            return false;
        } else if (o1 instanceof Tank && o2 instanceof Bullet) {
            collide(o2, o1);
        }
        return false;
    }
}
